package com.billard.controller;

import com.billard.entity.Role;
import com.billard.service.RoleService;
import com.billard.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色控制器
 */
@RestController
@RequestMapping("")
@Tag(name = "角色管理", description = "角色相关接口")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 查询所有角色
     */
    @GetMapping("/roles")
    @PreAuthorize("@securityPermission.hasPermission('role:view')")
    @Operation(summary = "查询所有角色", description = "查询所有角色")
    public Result<List<Role>> getRoles() {
        return Result.success(roleService.getRoles());
    }

    /**
     * 创建角色
     */
    @PostMapping("/roles")
    @PreAuthorize("@securityPermission.hasPermission('role:add')")
    @Operation(summary = "创建角色", description = "创建角色")
    public Result<Void> createRole(@RequestBody Role role) {
        roleService.createRole(role);
        return Result.success();
    }

    /**
     * 更新角色
     */
    @PutMapping("/roles")
    @PreAuthorize("@securityPermission.hasPermission('role:update')")
    @Operation(summary = "更新角色", description = "更新角色")
    public Result<Void> updateRole(@RequestBody Role role) {
        roleService.updateRole(role);
        return Result.success();
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/roles/{id}")
    @PreAuthorize("@securityPermission.hasPermission('role:delete')")
    @Operation(summary = "删除角色", description = "删除角色")
    public Result<Void> deleteRole(@PathVariable Long id) {
        roleService.deleteRole(id);
        return Result.success();
    }
}
